package com.alphas.user_centbackendalphas.controller;

import com.alphas.user_centbackendalphas.common.BaseResponse;
import com.alphas.user_centbackendalphas.common.StatusCode;
import com.alphas.user_centbackendalphas.exception.BusinessException;
import com.alphas.user_centbackendalphas.model.domain.User;
import com.alphas.user_centbackendalphas.model.domain.request.UserLoginRequest;
import com.alphas.user_centbackendalphas.model.domain.request.UserRegisterRequest;
import com.alphas.user_centbackendalphas.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *用户接口
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param userRegisterRequest 用户注册请求体
     * @return 注册成功后返回用户id
     */
    @PostMapping ("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if (userRegisterRequest == null){
            throw new BusinessException(StatusCode.NULL_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)){
            throw new BusinessException(StatusCode.NULL_ERROR);
        }
        return userService.userRegister(userAccount,userPassword,checkPassword);
    }

    /**
     * 用户登录
     * @param userLoginRequest  用户登录请求体
     * @param request 前端请求
     * @return 脱敏过的用户信息
     */
    @PostMapping ("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if (userLoginRequest == null){
            throw new BusinessException(StatusCode.NULL_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)){
            throw new BusinessException(StatusCode.NULL_ERROR);
        }
        return userService.userLogin(userAccount,userPassword,request);
    }

    /**
     * 用户注销
     * @param request
     * @return  是否成功注销
     */
    @PostMapping("logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        return userService.userLogout(request);
    }

    /**
     * 用户查找
     * @param request  前端请求
     * @param username 用户昵称
     * @return 查找后的用户列表
     */
    @GetMapping("/search")
    public BaseResponse<List<User>> userSearch(HttpServletRequest request, String username) {
        return userService.userSearch(request, username);
    }

    /**
     * 用户删除
     * @param request
     * @param userId 用户的id
     * @return 是否成功删除
     */
    @PostMapping("delete")
    public BaseResponse<Boolean> deleteUser(HttpServletRequest request, @RequestBody long userId) {
        if (userId < 0)
            throw new BusinessException(StatusCode.PARAM_ERROR);
        return userService.deleteUser(request, userId);
    }

    /**
     * 获取当前用户信息
     * @param request
     * @return 用户信息
     */
    @GetMapping("current")
    public BaseResponse<User> getCurrentUserInfo(HttpServletRequest request) {
        return userService.getCurrentUserInfo(request);
    }

}
